﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;
using YJKRoom.RoomTagModule;
using YJKView;

namespace YJKRoom.LabelRoomCenter
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdLableRoomCenter : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)15;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			Transaction transaction = new Transaction(this.m_doc, "LableRoomCenter");
			Result result;
			for (;;)
			{
				transaction.Start();
				RoomTagFilter roomTagFilter = new RoomTagFilter();
				IList<Element> list = null;
				try
				{
					list = this.m_selection.PickElementsByRectangle(roomTagFilter, "请框选房间标记对象");
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				catch (Exception)
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				try
				{
					foreach (Element element in list)
					{
						RoomTag roomTag = element as RoomTag;
						if (roomTag != null)
						{
							this.LableRoomCenter(roomTag);
						}
					}
				}
				catch (Exception)
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				transaction.Commit();
			}
			return result;
		}

		private void LableRoomCenter(RoomTag tag)
		{
			CurveArray curveArray = new CurveArray();
			Room room = tag.Room;
			IList<IList<BoundarySegment>> boundarySegments = room.GetBoundarySegments(new SpatialElementBoundaryOptions());
			foreach (IList<BoundarySegment> list in boundarySegments)
			{
				foreach (BoundarySegment segment in list)
				{
					Curve curve = segment.GetCurve();
					curveArray.Append(curve);
				}
			}
			XYZ xyz = (tag.Location as LocationPoint).Point;
			if (!CentroidByView.GetViewCentroidPoint(ref xyz, curveArray))
			{
				xyz = Geometry.Centroid(curveArray);
			}
			XYZ xyz2 = xyz;
			if (CentroidByView.GetAdjustCentroidPoint(ref xyz2, curveArray, this.m_doc))
			{
				xyz = xyz2;
			}
			List<XYZ> list2 = new List<XYZ>();
			foreach (object obj in curveArray)
			{
				Curve curve2 = (Curve)obj;
				list2.AddRange(curve2.Tessellate());
			}
			if (CentroidByView.IsPointInCurves(xyz, curveArray) == 1)
			{
				LinkElementId linkElementId = new LinkElementId(tag.Room.Id);
				UV uv = new UV(xyz.X, xyz.Y);
				RoomTag roomTag = this.m_doc.Create.NewRoomTag(linkElementId, uv, this.m_doc.ActiveView.Id);
				roomTag.RoomTagType = tag.RoomTagType;
				this.m_doc.Delete(tag.Id);
			}
		}

		private Document m_doc;

		private Selection m_selection;
	}
}
